import 'package:flutter/material.dart';
import 'package:hospitable_rental/models/general_type.dart';
import 'package:hospitable_rental/utils/utils.dart';
import 'package:hospitable_rental/widgets/filter_bar/data.dart';

class FilterBarViewModel extends ChangeNotifier {
  FilterBarResult get result {
    return FilterBarResult(
      areaId: _areaType.id,
      rentTypeId: _rentType.id,
      priceId: _priceType.id,
      moreIds: _selectedList.toList(),
    );
  }

  GeneralType _areaType = GeneralType("区域", "");
  GeneralType _rentType = GeneralType("方式", "");
  GeneralType _priceType = GeneralType("租金", "");

  GeneralType get areaType => _areaType;
  GeneralType get rentType => _rentType;
  GeneralType get priceType => _priceType;

  set areaType(GeneralType newValue) {
    _areaType = newValue;
    notifyListeners();
  }

  set rentType(GeneralType newValue) {
    _rentType = newValue;
    notifyListeners();
  }

  set priceType(GeneralType newValue) {
    _priceType = newValue;
    notifyListeners();
  }

  bool _isAreaActive = false;
  bool _isRentTypeActive = false;
  bool _isPriceActive = false;

  bool get isAreaActive => _isAreaActive;
  bool get isRentTypeActive => _isRentTypeActive;
  bool get isPriceActive => _isPriceActive;

  set isAreaActive(bool newValue) {
    _isAreaActive = newValue;
    notifyListeners();
  }

  set isRentTypeActive(bool newValue) {
    _isRentTypeActive = newValue;
    notifyListeners();
  }

  set isPriceActive(bool newValue) {
    _isPriceActive = newValue;
    notifyListeners();
  }

  List<GeneralType> _roomTypeList = [];
  List<GeneralType> _orientedList = [];
  List<GeneralType> _floorList = [];

  Set<String> _selectedList = Set();

  List<GeneralType> get roomTypeList => _roomTypeList;
  List<GeneralType> get orientedList => _orientedList;
  List<GeneralType> get floorList => _floorList;

  Map<String, List<GeneralType>> get dataList {
    var result = Map<String, List<GeneralType>>();
    result["roomTypeList"] = _roomTypeList;
    result["orientedList"] = _orientedList;
    result["floorList"] = _floorList;
    return result;
  }

  set dataList(Map<String, List<GeneralType>> data) {
    _roomTypeList = data["roomTypeList"] ?? [];
    _orientedList = data["orientedList"] ?? [];
    _floorList = data["floorList"] ?? [];
    notifyListeners();
  }

  Set<String> get selectedList => _selectedList;

  selectedListToggleItem(String? value) {
    if (_selectedList.contains(value)) {
      _selectedList.remove(value);
    } else {
      if (!stringIsNullOrEmpty(value)) selectedList.add(value!);
    }
    notifyListeners();
  }

  resetSelectedList() {
    if (_selectedList.isNotEmpty) {
      _selectedList.clear();
    }
    notifyListeners();
  }
}
